
import React from "react";
import Qs from 'qs'
import axios from "../../../../http";
import moment from 'moment';

import Calend, { CalendarView } from 'calend'
import 'calend/dist/styles/index.css';
import {OnNewEventClickData} from "calend";
import {OnEventClickData} from "calend";
import {OnPageChangeData} from "calend";
import {withRouter} from "react-router";
import {PRIVATE_ROUTE} from "../../../../route.constants";
import {splitCrossDateEvent} from "./EventUtils";
import {NavBar} from "antd-mobile";
class ScheduleOverview extends React.Component{

    constructor(props) {

        super(props);

        this.state={eventList:{},userList:[],userViewList:[],modalVisible:false,modalEditVisible:false,modalViewVisible:false,calendarWeekends:true};

    }
    onNewEventClick = (data: OnNewEventClickData) => {



    };
    onEventClick = (data: OnEventClickData) => {
        let originEvent=data.originEvent;
        let state={
            id:originEvent.id,
            originId: originEvent.originId,
            userViewList:[],
            title:originEvent.title,
            memo:originEvent.memo,
            startDate:originEvent.originStart.substring(0,10),
            endDate:originEvent.originEnd.substring(0,10),
            startTime:[originEvent.originStart.substring(11,13),originEvent.originStart.substring(14,16)],
            endTime:[originEvent.originEnd.substring(11,13),originEvent.originEnd.substring(14,16)],
            prePath:'/main/'+PRIVATE_ROUTE.APP_OA_WORK_SCHEDULE_OVERVIEW
        };
        for(let i=0;i<originEvent.simpleUserList.length;i++){
            let simpleUser=originEvent.simpleUserList[i];
            state.userViewList.push(simpleUser.realName+'_'+simpleUser.id)
        }

        this.props.history.push({pathname:"/main/"+PRIVATE_ROUTE.APP_OA_WORK_SCHEDULE_VIEW,state:state})

    };


    onPageChange = (data: OnPageChangeData) => {
        this.setState({"eventList":{}});
        axios.post('api/oa/work/event/findAll', Qs.stringify({
            start:moment(data.rangeFrom).format('YYYY-MM-DD HH:mm:ss'),
            end:moment(data.rangeTo).format('YYYY-MM-DD HH:mm:ss')
        }),{headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then((response)=>{

                let  eventList=response.data.data;

                eventList=splitCrossDateEvent(eventList);

                let stateEventList=this.state.eventList;

                for(let i=0;i<eventList.length;i++){

                    let event=eventList[i];

                    let eventDate=moment(event.start).format('DD-MM-YYYY HH:mm:ss').substring(0,10);

                    if(stateEventList[eventDate]==undefined){
                        let newEventList=new Array()
                        stateEventList[eventDate]=newEventList;
                    }
                    let newEvent= {
                        id: event.id,
                        startAt: moment(event.start).utc().format(),
                        endAt: moment(event.end).utc().format(),
                        summary: event.title,
                        color: (new Date(event.start))>(new Date())?'green':'gray',
                        originEvent:event

                    };
                    stateEventList[eventDate].push(newEvent);
                }
                this.setState({eventList:stateEventList})
            })
            .catch(function (error) {

            });
    }

    render(){
        return(
            <>
                <NavBar onBack={()=>{this.props.history.push({ pathname: "/" })}} >日程总览</NavBar>
                <Calend
                    onEventClick={this.onEventClick}
                    events={this.state.eventList}
                    initialDate={new Date().toISOString()}
                    hourHeight={60}
                    initialView={CalendarView.WEEK}
                    disabledViews={[CalendarView.DAY]}
                    onNewEventClick={this.onNewEventClick}
                    onPageChange={this.onPageChange}
                    onSelectView={this.onSelectView}

                />

            </>

        );
    }
}
export default withRouter(ScheduleOverview)